package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class SubarrayProductLessThanK {
    // 2022-08-22 12:20:17
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int numSubarrayProductLessThanK(int[] nums, int k) {
            if (k <= 1) {
                return 0;
            }
            int n = nums.length;
            int left = 0, right = 0;
            int ans = 0;
            int temp = 1;

            while (right < n) {
                temp *= nums[right];
                while (temp >= k) {
                    // 重构窗口
                    temp /= nums[left];
                    left++;
                }
                // 计算该窗口内以left开头的子数组有多少
                ans += right - left + 1;
                right++;   // 扩大窗口
            }
            return ans;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}